package com.google.gson.internal.bind;

import com.google.gson.b.a;
import com.google.gson.b.c;
import com.google.gson.b.d;
import com.google.gson.internal.ObjectConstructor;
import com.google.gson.j;
import com.google.gson.p;
import com.google.gson.q;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

final class k extends TypeAdapter {
    final /* synthetic */ j a;
    private final TypeAdapter b;
    private final TypeAdapter c;
    private final ObjectConstructor d;

    public k(j jVar, l lVar, Type type, TypeAdapter typeAdapter, Type type2, TypeAdapter typeAdapter2, ObjectConstructor objectConstructor) {
        this.a = jVar;
        this.b = new x(lVar, typeAdapter, type);
        this.c = new x(lVar, typeAdapter2, type2);
        this.d = objectConstructor;
    }

    private String b(j jVar) {
        if (jVar.i()) {
            p m = jVar.m();
            if (m.p()) {
                return String.valueOf(m.a());
            }
            if (m.o()) {
                return Boolean.toString(m.f());
            }
            if (m.q()) {
                return m.b();
            }
            throw new AssertionError();
        } else if (jVar.j()) {
            return "null";
        } else {
            throw new AssertionError();
        }
    }

    public Map a(a aVar) throws IOException {
        c f = aVar.f();
        if (f == c.NULL) {
            aVar.j();
            return null;
        }
        Map map = (Map) this.d.construct();
        Object b;
        if (f == c.BEGIN_ARRAY) {
            aVar.a();
            while (aVar.e()) {
                aVar.a();
                b = this.b.b(aVar);
                if (map.put(b, this.c.b(aVar)) != null) {
                    throw new q("duplicate key: " + b);
                }
                aVar.b();
            }
            aVar.b();
            return map;
        }
        aVar.c();
        while (aVar.e()) {
            b = this.b.a(new p(aVar.g()));
            if (map.put(b, this.c.b(aVar)) != null) {
                throw new q("duplicate key: " + b);
            }
        }
        aVar.d();
        return map;
    }

    public void a(d dVar, Map map) throws IOException {
        int i = 0;
        if (map == null) {
            dVar.f();
        } else if (this.a.b) {
            List arrayList = new ArrayList(map.size());
            List arrayList2 = new ArrayList(map.size());
            int i2 = 0;
            for (Entry entry : map.entrySet()) {
                j a = this.b.a(entry.getKey());
                arrayList.add(a);
                arrayList2.add(entry.getValue());
                int i3 = (a.g() || a.h()) ? 1 : 0;
                i2 = i3 | i2;
            }
            if (i2 != 0) {
                dVar.b();
                while (i < arrayList.size()) {
                    dVar.b();
                    com.google.gson.internal.k.a((j) arrayList.get(i), dVar);
                    this.c.a(dVar, arrayList2.get(i));
                    dVar.c();
                    i++;
                }
                dVar.c();
                return;
            }
            dVar.d();
            while (i < arrayList.size()) {
                dVar.a(b((j) arrayList.get(i)));
                this.c.a(dVar, arrayList2.get(i));
                i++;
            }
            dVar.e();
        } else {
            dVar.d();
            for (Entry entry2 : map.entrySet()) {
                dVar.a(String.valueOf(entry2.getKey()));
                this.c.a(dVar, entry2.getValue());
            }
            dVar.e();
        }
    }

    public /* synthetic */ Object b(a aVar) throws IOException {
        return a(aVar);
    }
}
